package com.RedRiver.hospitalsystem.personal_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.personal_service.common.Personal_serviceResultCode;
import com.RedRiver.hospitalsystem.personal_service.common.exception.AccountException;
import com.RedRiver.hospitalsystem.personal_service.common.util.Md5Util;
import com.RedRiver.hospitalsystem.personal_service.common.util.TokenTemplate;
import com.RedRiver.hospitalsystem.personal_service.infra.mapper.AccountMapper;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Account;
import com.RedRiver.hospitalsystem.personal_service.service.AccountService;
import com.RedRiver.hospitalsystem.personal_service.service.Form.AccountForm;
import com.RedRiver.hospitalsystem.personal_service.service.Form.Login;
import com.RedRiver.hospitalsystem.personal_service.service.Form.PageForm;
import com.RedRiver.hospitalsystem.personal_service.service.info.AccountPageInfo;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private TokenTemplate tokenTemplate;

    /**
     * 分页查询用户
     *
     * @param
     * @return
     */
    @Override
    public IPage<AccountPageInfo> findPageAllAccount(PageForm form) {
        Page<Account> accountInfoPage = new Page<>();
        accountInfoPage = this.baseMapper.selectPage(new Page<Account>(form.getPage() == 0 ? 1 : form.getPage(), form.getPageSize() == 0 ? 2 : form.getPageSize()), null);
        List<Account> accountInfoList = accountInfoPage.getRecords();
        List<AccountPageInfo> accountPageInfoList = BeanUtil.copyToList(accountInfoList, AccountPageInfo.class);
        Page<AccountPageInfo> page = new Page<>();
        page.setRecords(accountPageInfoList);
        page.setCurrent(accountInfoPage.getCurrent());
        page.setSize(accountInfoPage.getSize());
        page.setTotal(accountInfoPage.getTotal());
        return page;
    }

    /**
     * 医生登录
     *
     * @param loginAccount
     * @return
     */
    @Override
    public Result<String> login(Login loginAccount) {
        System.out.println(loginAccount);
        if (StringUtils.isEmpty(loginAccount.getUsername()) && StringUtils.isEmpty(loginAccount.getPassword()))
            throw new AccountException(Personal_serviceResultCode.ACCOUNT_ACCOUNT_NULL);
        Account account = accountMapper.selectOne(new QueryWrapper<Account>().eq("account_name", loginAccount.getUsername()));
        if (null == account) throw new AccountException(Personal_serviceResultCode.ACCOUNT_USERNAME_ERROR);
        if (!account.getAccountPassword().equals(loginAccount.getPassword()))
            throw new AccountException(Personal_serviceResultCode.ACCOUNT_PASSWORD_ERROR);
        //生成token
        Map<String, Object> maps = new HashMap<>();
        maps.put("accountId", account.getAccountId());
        maps.put("accountName", account.getAccountName());
        String token = tokenTemplate.createToken(maps);
        System.out.println(token);
        return Result.getInstance(String.class).setData(token).setMsg("登录成功");
    }


    /**
     * 新增用户
     *
     * @param accountForm 传入的用户信息
     * @return
     */
    @Override
    public Result createAccount(AccountForm accountForm) {
        Account account_name = accountMapper.selectOne(new QueryWrapper<Account>().eq("account_name", accountForm.getAccountName()));
        if (account_name != null) throw new AccountException(Personal_serviceResultCode.ACCOUNT_USERNAME_EXISTED);
        if (accountForm.getAccountType() != 0 && accountForm.getAccountType() != 1)
            throw new AccountException(Personal_serviceResultCode.ACCOUNT_TYPE_NOTEXISTED);
        int insert = accountMapper.insert(BeanUtil.copyProperties(accountForm, Account.class));
        if (insert == 0) throw new AccountException(Personal_serviceResultCode.ACCOUNT_ADDITION_FAILD);
        return Result.getInstance().setMsg("新增成功").setCode(200);
    }

    /**
     * 更新用户信息
     *
     * @param
     * @return
     */
    @Override
    public Result update(AccountForm accountForm) {
        Account account = BeanUtil.copyProperties(accountForm, Account.class);
        Account account_name = accountMapper.selectOne(new QueryWrapper<Account>().eq("account_name", account.getAccountName()));
        if (account_name == null) throw new AccountException(Personal_serviceResultCode.ACCOUNT_USER_NOEXISTED);
        if (accountMapper.update(account, new QueryWrapper<Account>().eq("account_name", account.getAccountName())) == 0)
            throw new AccountException(Personal_serviceResultCode.ACCOUNT_UPDATE_FAILD);
        return Result.getInstance().setMsg("用户信息更新成功").setCode(200);
    }

    /**
     * 根据id来删除用户
     *
     * @param accountId
     * @return
     */
    @Override
    public Result delete(Integer accountId) {
        int i = accountMapper.deleteById(accountId);
        if (i == 0) throw new AccountException(Personal_serviceResultCode.ACCOUNT_DELETE_FAILD);
        return Result.getInstance().setCode(200).setMsg("删除成功");
    }


}
